﻿package com.valeosys {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.net.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.ByteArray;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.text.TextField;
    public class UploadImage extends Sprite {
        public var jagFileRefSave:FileReference = new FileReference();
        private var loader:Loader = new Loader();
		private var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
		private var textFilter:FileFilter = new FileFilter("Text Files (*.php,*.xml)", "*.php; *.xml");
		public var imgContainer:ImgContainer;
		private var allTypes:Array;
		private var UrlRequest:URLRequest;
		public var bitmapData:BitmapData;
		
		public var file_name:String;
		public var file_size:String;
		public var fileDisplay_txt:String;
		public var status_txt:String;

    public function UploadImage() {
        super();
		allTypes = new Array(imagesFilter, textFilter);
		UrlRequest = new URLRequest("uploader_script.php");
        //upload_Image.addEventListener(MouseEvent.CLICK,onClickSave);
		//UploadButton.addEventListener(MouseEvent.CLICK,onClickSave);
		///upload_btn.addEventListener(MouseEvent.CLICK, uploadVars);
		//jagFileRefSave.addEventListener(ProgressEvent.PROGRESS, progressHandler);
	}
    public function onClickSave(e:MouseEvent):void{
		trace("onClickSave");
		file_name = "";
		file_size = "";
		if(getChildByName("ImgContainer") != null)
		{
			removeChild(getChildByName("ImgContainer"));
		}
        jagFileRefSave.browse(allTypes);
        jagFileRefSave.addEventListener(Event.SELECT, selectedFile);
    }
    public function selectedFile(e:Event):void {
		trace("selectedFile");
		fileDisplay_txt = jagFileRefSave.name;
		jagFileRefSave.load();
		if((jagFileRefSave.type == ".php") ||  (jagFileRefSave.type == ".xml")) {
			file_name = "Selected file name is : "+jagFileRefSave.name;
			file_size = "Selected file size is : "+(jagFileRefSave.size/1024)+" KB";
			trace(file_name);
			trace(file_size);
		} else {
	      	jagFileRefSave.addEventListener(Event.COMPLETE, loaded);
		}
    }
	private function uploadVars(e:MouseEvent):void {
		trace("uploadVars");
		if(fileDisplay_txt != "") {
			jagFileRefSave.upload(UrlRequest);
			status_txt = jagFileRefSave.name + " has been uploaded.";
			fileDisplay_txt = "";
		}
	}
    private function loaded(e:Event):void{
		trace("loaded");
        var rawBytes:ByteArray = jagFileRefSave.data;
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData)
        loader.loadBytes(rawBytes);
    }
    public function getBitmapData(e:Event):void {
		trace("getBitmapData");
		imgContainer = new ImgContainer();
		imgContainer.name = "imgContainer";
        imgContainer.addChild(loader);
		//bitmapData = Bitmap(loader.contentLoaderInfo.content).bitmapData;
		imgContainer.width = 150;
		imgContainer.height = 150;
		//imgContainer.x = stage.stageWidth/2 - imgContainer.width/2;
		//imgContainer.y = stage.stageHeight/4;
		imgContainer.x = 200;
		imgContainer.y = 200;
		//addChild(imgContainer);
		//addChild(loader);
		var cEvt:CustomEvent = new CustomEvent("onBitmapReady");
		cEvt.customMessage = "Bitmap ready!";
		dispatchEvent(cEvt);
    }
	}
}
